/**
 * @file    test_fixed_rand.c
 * @brief   Example demonstrating fixed/deterministic random number generator
 * 
 * This program demonstrates how to use the fixed random number generator
 * similar to OpenSSL's FuzzerSetRand() function.
 * 
 * Usage:
 *   ./test_fixed_rand [enable]
 * 
 * If "enable" is provided, fixed random will be enabled.
 */

#include "app.h"
#include "matrixssl/matrixsslApi.h"
#include <stdio.h>

int main(int argc, char **argv)
{
    unsigned char buf1[32], buf2[32], buf3[32];
    int i;
    
    /* Initialize MatrixSSL */
    if (matrixSslOpen() < 0) {
        printf("Failed to initialize MatrixSSL\n");
        return -1;
    }
    
    printf("=== MatrixSSL Fixed Random Number Generator Test ===\n\n");
    
    /* Test 1: Normal random (system entropy) */
    printf("Test 1: Normal random (system entropy)\n");
    if (psGetEntropy(buf1, sizeof(buf1), NULL) > 0) {
        printf("Random bytes: ");
        for (i = 0; i < sizeof(buf1); i++) {
            printf("%02x ", buf1[i]);
        }
        printf("\n\n");
    }
    
    /* Test 2: Enable fixed random */
    if (argc > 1 && strcmp(argv[1], "enable") == 0) {
        printf("Test 2: Enabling fixed random (deterministic)\n");
        psSetFixedRand();
        printf("Fixed random enabled: %d\n", psIsFixedRandEnabled());
        
        if (psGetEntropy(buf2, sizeof(buf2), NULL) > 0) {
            printf("Fixed random bytes: ");
            for (i = 0; i < sizeof(buf2); i++) {
                printf("%02x ", buf2[i]);
            }
            printf("\n");
        }
        
        /* Reset and get again - should get same sequence */
        printf("\nTest 3: Reset and get again (should start from 1)\n");
        psResetFixedRand();
        if (psGetEntropy(buf3, sizeof(buf3), NULL) > 0) {
            printf("Reset random bytes: ");
            for (i = 0; i < sizeof(buf3); i++) {
                printf("%02x ", buf3[i]);
            }
            printf("\n");
        }
        
        /* Disable fixed random */
        printf("\nTest 4: Disabling fixed random\n");
        psUnsetFixedRand();
        printf("Fixed random enabled: %d\n", psIsFixedRandEnabled());
    } else {
        printf("To test fixed random, run: %s enable\n", argv[0]);
    }
    
    matrixSslClose();
    return 0;
}
